<!--
- Author:  LiShibin.
- Date:    2017/10/19 0019.
- File:    粉丝列表（选择粉丝为共享成员）.
-->
<template>
  <div class="Web_Box">
    <div
      v-infinite-scroll="loadMore"
      infinite-scroll-disabled="false"
      infinite-scroll-distance="0"
      infinite-scroll-immediate-check="false">
      <div class="bg_fff mb10" v-for="fans in fansList.data">
        <div class="diary">
          <ul class=" f-cb">
            <li class="imgs"  @click="gotoBlog(fans.member_id)"><a><img :src="fans.member_avatar ? $config.imgsite + $config.avatarSrc + fans.member_avatar + '_w150': defaultAvatar"></a></li>
            <li class="pl20 w400" style="height: 34px; line-height: 34px"><a>
            <h3 class="d_tit">{{fans.member_name}}</h3>
              <!--<p class="slh">{{fans.fans_num}}位粉丝</p>-->
            </a>
            </li>
            <!--<li><a href="" class="f_close">取消关注</a></li>-->
            <li>
              <i class="iconfont icon-select" @click="switchSelected(fans, false)" v-show="fans.selected"></i>
              <i class="iconfont icon-pitch2" @click="switchSelected(fans, true)" v-show="!fans.selected"></i>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="artic_btn f-cb">
      <a class=" bg_a8a8a8" @click="back">取消</a><a class="bg_3dbb6e" @click="addGardenMember">确定</a>
    </div>
    <loading-more :allLoaded="fansList.allLoaded" :show="fansList.data.length > 0"></loading-more>
    <empty-data :show="fansList.allLoaded && fansList.data.length <= 0" text="当前没有可添加的用户"></empty-data>
    <!--<footer-tab></footer-tab>-->
  </div>
</template>

<script>

  export default{
    data() {
      return {
        gardenId: this.$route.query.garden,
        defaultAvatar: require('../../../assets/images/avatar.png'),
        fansList: {requested: false, loading: false, allLoaded: false, page: 1, pageSize: 10, data: []}
      }
    },
    components: {},
    created() {
      this.getFansList(this.fansList.page, this.fansList.pageSize)
    },
    methods: {
      back() {
        this.$router.go(-1)
      },
      gotoBlog(memberId) {
        this.$router.push({name: 'blog-index', query: {member: memberId}})
      },
      gotoGoodsDetail(storeId, id) {
        this.$router.push({name: 'goods-detail', params: {id: id}, query: {storeId: storeId}})
      },
      switchSelected(fans, boolValue) {
        if (fans.selected) {
          fans.selected = boolValue
        } else {
          this.$set(fans, 'selected', boolValue)
        }
      },
      addGardenMember() {
        let memberIdList = []
        for (let i = 0; i < this.fansList.data.length; i++) {
          if (this.fansList.data[i].selected) {
            memberIdList.push(this.fansList.data[i].member_id)
          }
        }
        console.log(this.fansList, memberIdList)
        if (memberIdList.length <= 0) {
          this.$toast('请选择成员')
          return
        }
        this.$request.addGardenMember(this.gardenId, memberIdList).then(data => {
          this.$toast('添加成功')
          this.$router.go(-1)
        }).catch(e => {
          this.$toast('添加失败:' + e.msg)
        })
      },
      getFansList(page, pageSize) {
        this.fansList.loading = true
        this.$request.getFansListUnAddToGarden(this.gardenId, page, pageSize).then(data => {
          if (data.length < this.fansList.pageSize) {
            this.fansList.allLoaded = true
          }
          this.fansList.data = [...this.fansList.data, ...data]
        }).finally(() => {
          this.fansList.requested = true
          this.fansList.loading = false
        })
      },
      loadMore() {
        if (this.fansList.loading || this.fansList.allLoaded) { // 正在加载 或 已取得全部数据
          return
        }
        setTimeout(() => {
          this.getFansList(++this.fansList.page, this.fansList.pageSize)
        }, 500)
      },
      delFans(id) {
        this.$messageBox.confirm(`你确定要删除吗?`).then(action => {
          this.$request.delFans(id).then(data => {
            this.$toast('删除收藏成功')
            for (let i = 0; i < this.fansList.data.length; i++) {
              if (this.fansList.data[i].goods_commonid == id) {
                this.fansList.data.splice(i, 1)
              }
            }
          }).catch(e => {
            this.$toast('删除收藏失败：' + e.msg)
          })
        }).catch(() => {
        })
      }
    }
  }
</script>

<style>

</style>